<?php

class Twis_Handler_Login extends Twis_Handler_Base
{
    public function execute()
    {
        $request = $this->getRequest();
        $template = $this->getTemplate();
        if($request->isPost()) {
            $username = $request->getPost('username');
            $password = $request->getPost('password');
            $remember = $request->getPost('remember');
            $proxy = $this->getProxy();
            try {
                $response = $this->getResponse();
                $sessionKey = $proxy->call('social.user.login', array('username' => $username, 'password' => $password));
                $this->setSessionKey($sessionKey, $remember);
                $url = $this->getUrl()->href('index');
                return $response->redirect($url);
            } catch(InvalidArgumentException $e) {
                $error = array();
                $error['code'] = $e->getCode();
                $error['message'] = $e->getMessage();
                $template->set('error', $error);
            }
        }
        $template->set('form', $request->getPost());
        return $template->render('user/login.html');
    }
}
